/*
 */
package it.pride.primefacesShowcase.controller;

import it.pride.primefacesShowcase.filters.Filter;
import it.pride.primefacesShowcase.model.User;
import it.pride.primefacesShowcase.service.UserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 * Extended Lazy Data Model which allows additional filters to be specificed.
 * quick & dirty way to expand on the basic Primefaces lazy filters which only allows String values 
 * @author eluibon
 */
public class ExtUserLazyDataModel extends LazyDataModel<User> {

    private Logger logger = Logger.getLogger(this.getClass().getName());

    UserService userService;

    List<Filter> additionalFilters;

    public void init(UserService userService) {

        this.userService = userService;
        additionalFilters = null;
    }

    public ExtUserLazyDataModel() {
    }

    @Override
    public User getRowData(String rowKey) {

        return userService.findById(Long.valueOf(rowKey));
    }

    @Override
    public Object getRowKey(User user) {
        return user.getId();
    }

    @Override
    public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {

        this.setRowCount(userService.countForExtLazyDatatable(filters, additionalFilters));
        return userService.findForExtLazyDatatable(first, pageSize, sortField, sortOrder, filters, additionalFilters);
    }

    public List<Filter> getAdditionalFilters() {
        return additionalFilters;
    }

    public void setAdditionalFilters(List<Filter> additionalFilters) {
        this.additionalFilters = additionalFilters;
    }
}
